/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jefoutta
 */
import java.util.*;

public class And extends LogicGate {

    public And(String tname, int numEntries){
        this.numEntries = numEntries;
        this.input = new int[numEntries];
        this.inputAvailable = new boolean[numEntries];
        this.delayDown = new int[numEntries];
        this.delayUp = new int[numEntries];
        this.outputChannels = new ArrayList(0);
        this.inputChannels = new ArrayList();
        this.type = 1;
        for(int i = 0; i < input.length; i++){
            input[i] = -1;
            this.inputAvailable[i] = true;
        }
        this.output = -1;
        this.error = false;
        this.errorCount = 0;
        this.pathCount = 0;
        this.name = tname;
    }

    public int getOutput(){
        int i = 0;
        int temp = -2;
        while(i < this.numEntries){
            temp = this.input[i];
            if(temp == 0)
                break;
            i++;
        }
        return temp;
    }

    public void sensitizeEntry(int index){
        this.input[index] = 2;
        int i = 0;
        while(i < this.numEntries){
            if(i != index){
                this.input[i] = 1;
            }
            i++;
        }
    }

    public void justifyFromOutput(int outputVal){
        System.out.println("entro a justify " + this.name);
        this.output = outputVal;
        int temp = -1;
        if((this.input[0] != -1)&&(this.input[1] != -1))
            this.isCorrect();
        else{
            switch(this.input[0]){
                case -1:{
                    switch(this.input[1]){
                        case -1:{
                            if(this.output == 1){
                                this.input[0] = 1;
                                this.propagateIn(0);
                                this.input[1] = 1;
                                this.propagateIn(1);
                            }else{
                                this.setError(true);
                            }
                        }break;
                        case 0:{
                            if(this.output == 0){
                                this.input[0] = 2;
                                this.propagateIn(0);
                            }
                            if(this.output == 1)
                                this.setError(true);
                            if(this.output == 2)
                                this.setError(true);
                        }break;
                        case 1:{
                            if(this.output == 0){
                                this.input[0] = 0;
                                this.propagateIn(0);
                            }
                            if(this.output == 1){
                                this.input[0] = 1;
                                this.propagateIn(0);
                            }
                            if(this.output == 2){
                                this.input[0] = 2;
                                this.propagateIn(0);
                            }
                        }break;
                        case 2:{
                            if(this.output == 0){
                                this.input[0] = 0;
                                this.propagateIn(0);
                            }
                            if(this.output == 1)
                                this.setError(true);
                            if(this.output == 2){
                                this.input[0] = 1;
                                this.propagateIn(0);
                            }
                        }break;
                    }
                }break;
                case 0:{
                    switch(this.input[1]){
                        case -1:{
                            if(this.output == 0){
                                this.input[1] = 2;
                                this.propagateIn(1);
                            }
                            if(this.output == 1)
                                this.setError(true);
                            if(this.output == 2)
                                this.setError(true);
                        }break;
                        case 0:{
                            if(this.output != this.getOutput())
                                this.setError(true);
                        }break;
                        case 1:{
                            if(this.output != this.getOutput())
                                this.setError(true);
                        }break;
                        case 2:{
                            if(this.output != 0)
                                this.setError(true);
                        }break;
                    }
                }break;
                case 1:{
                    switch(this.input[1]){
                        case -1:{
                            if(this.output == 0){
                                this.input[1] = 0;
                                this.propagateIn(1);
                            }
                            if(this.output == 1){
                                this.input[1] = 1;
                                this.propagateIn(1);
                            }
                            if(this.output == 2){
                                this.input[1] = 2;
                                this.propagateIn(1);
                            }
                        }break;
                        case 0:{
                            if(this.output != this.getOutput())
                                this.setError(true);
                        }break;
                        case 1:{
                            if(this.output != this.getOutput())
                                this.setError(true);
                        }break;
                        case 2:{
                            if(this.output != 2)
                                this.setError(true);
                        }break;
                    }
                }break;
                case 2:{
                    switch(this.input[1]){
                        case -1:{
                            if(this.output == 0){
                                this.input[1] = 0;
                                this.propagateIn(1);
                            }
                            if(this.output == 1)
                                this.setError(true);
                            if(this.output == 2){
                                this.input[1] = 1;
                                this.propagateIn(1);
                            }
                        }break;
                        case 0:{
                            if(this.output != 0)
                                this.setError(true);
                        }break;
                        case 1:{
                            if(this.output != 2)
                                this.setError(true);
                        }break;
                        case 2:{
                            this.setError(true);
                        }break;
                    }
                }break;
            }
        }
        if(this.bigError){
            System.out.println("salio con error de justify " +this.name);
        }else System.out.println("salio con exito de justify");
    }

    public boolean isCorrect(){
        int outputVal = this.getOutput();
        if (this.output == outputVal)
            return true;
        return false;
    }
}